﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using AutoWelding.BLL;
using AutoWelding.Models;
using AutoWelding.Utils;

namespace AutoWelding.Views.Recipes
{
    public partial class RecipeInfoForm : Form
    {
        public RecipeInfoForm()
        {
            InitializeComponent();
        }
        RecipesBLL recipesBLL = new RecipesBLL();
        public event Action ReLoadList;//刷新列表页数据
        public AddOrUpdateModel typeModel = null;

        private void RecipeInfoForm_Load(object sender, EventArgs e)
        {
            if(this.Tag != null)
            {
                typeModel = this.Tag as AddOrUpdateModel;
                InitRecipeInfo();
            }
        }
        /// <summary>
        /// 初始化配方页面信息
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitRecipeInfo()
        {
           if(typeModel != null)
            {
                if(typeModel.ActType == 0)
                {
                    //查看详情
                    GetRecipeTails(typeModel.FId);
                    //初始化产品 下拉框
                    RecipesModel recipes = recipesBLL.GetRecipesById(typeModel.FId);
                    InitProductComboBox(recipes.ProductId);
                    btn_commit.Visible = false;
                    panel1.Enabled = false;
                }
                else if(typeModel.ActType == 1)
                {
                    //编辑
                    GetRecipeTails(typeModel.FId);
                    //初始化产品 下拉框
                    RecipesModel recipes = recipesBLL.GetRecipesById(typeModel.FId);
                    InitProductComboBox(recipes.ProductId);
                    btn_commit.Visible = true;
                    btn_commit.Text = "修改";
                    panel1.Enabled = true;
                    //修改逻辑
                }
                else if(typeModel.ActType == 2)
                {
                    //新增
                    //初始化产品 下拉框
                    InitProductComboBox(0);
                    //清空
                    //ClearInfo();
                    label3.Visible = false;
                    label4.Visible = false;
                    label_createtime.Visible = false;
                    label_updatetime.Visible = false;
                    btn_commit.Visible = true;
                    btn_commit.Text = "新增";
                    panel1.Enabled = true;

                }
            }
        }
        /// <summary>
        /// 清空数据
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void ClearInfo()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 查看细节
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void GetRecipeTails(int recipeId)
        {
            RecipesModel recipesModel = recipesBLL.GetRecipesById(recipeId);
            if(recipesModel != null)
            {
                txt_recipeName.Text = recipesModel.RecipeName;
                label_createtime.Text = recipesModel.CreateTime.ToString();
                label_updatetime.Text = recipesModel.UpdateTime.ToString();
                txt_remark.Text = recipesModel.Remark;
               // txt_weldingNum.Text = recipesModel.Recipe01;

                txt_weldingNum.Text = recipesModel.WeldingNum;
                txt_recipe02.Text = recipesModel.Recipe02;
                txt_recipe03.Text = recipesModel.Recipe03;
                txt_recipe04.Text = recipesModel.Recipe04;
                txt_recipe05.Text = recipesModel.Recipe05;
                txt_recipe06.Text = recipesModel.Recipe06;
                txt_recipe07.Text = recipesModel.Recipe07;
                txt_recipe08.Text = recipesModel.Recipe08;
                txt_recipe09.Text = recipesModel.Recipe09;
                txt_recipe10.Text = recipesModel.Recipe10;

                txt_recipe11.Text = recipesModel.Recipe11;
                txt_recipe12.Text = recipesModel.Recipe12;
                txt_recipe13.Text = recipesModel.Recipe13;
                txt_recipe14.Text = recipesModel.Recipe14;
                txt_recipe15.Text = recipesModel.Recipe15;
                txt_recipe16.Text = recipesModel.Recipe16;
                txt_recipe17.Text = recipesModel.Recipe17;
                txt_recipe18.Text = recipesModel.Recipe18;
                txt_recipe19.Text = recipesModel.Recipe19;
                txt_recipe20.Text = recipesModel.Recipe20;

            }
        }

        /// <summary>
        /// 初始化产品下拉框
        /// </summary>
        /// <param name="productId"></param>
        private void InitProductComboBox(int productId)
        {
            ProductsBLL productsBLL = new ProductsBLL();
            // 获取产品列表
            List<ProductsModel> productsList = productsBLL.GetProductsList("", false);

            // 创建默认项
            ProductsModel defaultItem = new ProductsModel
            {
                ProductId = 0, // 使用特殊ID表示默认项
                ProductName = "---请选择---"
            };

            // 在列表前面插入默认项
            productsList.Insert(0, defaultItem);

            // 设置数据源
            cbx_productName.DataSource = productsList;

            // 设置显示成员（下拉框中显示的文本）
            cbx_productName.DisplayMember = "ProductName";

            // 设置值成员（下拉框中每个选项的值）
            cbx_productName.ValueMember = "ProductId";

            // 设置默认选中项（可选）
            cbx_productName.SelectedIndex = productId;
        }

        /// <summary>
        /// 关闭按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 提交按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_commit_Click(object sender, EventArgs e)
        {
            if(typeModel.ActType == 1)
            {
                //修改
                UpdateRecipe();
            }
            else if(typeModel.ActType == 2)
            {
                //新增
                InsertRecipe();
            }
        }

        /// <summary>
        /// 修改配方数据
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        //private void UpdateRecipe()
        //{
        //    if (!ValidateForm())
        //        return;

        //    try
        //    {
        //        RecipesModel recipe = recipesBLL.GetRecipesById(typeModel.FId);
        //        if (recipe == null)
        //        {
        //            MessageBox.Show("找不到该配方信息！", "错误");
        //            return;
        //        }

        //        recipe.RecipeName = txt_recipeName.Text.Trim();
        //        recipe.ProductId = (int)cbx_productName.SelectedValue;
        //        recipe.WeldingNum = txt_weldingNum.Text.Trim();
        //        recipe.Recipe02 = txt_recipe02.Text.Trim();
        //        recipe.Recipe03 = txt_recipe03.Text.Trim();
        //        recipe.Recipe04 = txt_recipe04.Text.Trim();
        //        recipe.Recipe05 = txt_recipe05.Text.Trim();
        //        recipe.Recipe06 = txt_recipe06.Text.Trim();
        //        recipe.Recipe07 = txt_recipe07.Text.Trim();
        //        recipe.Recipe08 = txt_recipe08.Text.Trim();
        //        recipe.Recipe09 = txt_recipe09.Text.Trim();
        //        recipe.Recipe10 = txt_recipe10.Text.Trim();

        //        recipe.Recipe11 = txt_recipe11.Text.Trim();
        //        recipe.Recipe12 = txt_recipe12.Text.Trim();
        //        recipe.Recipe13 = txt_recipe13.Text.Trim();
        //        recipe.Recipe14 = txt_recipe14.Text.Trim();
        //        recipe.Recipe15 = txt_recipe15.Text.Trim();
        //        recipe.Recipe16 = txt_recipe16.Text.Trim();
        //        recipe.Recipe17 = txt_recipe17.Text.Trim();
        //        recipe.Recipe18 = txt_recipe18.Text.Trim();
        //        recipe.Recipe19 = txt_recipe19.Text.Trim();
        //        recipe.Recipe20 = txt_recipe20.Text.Trim();

        //        recipe.Remark = txt_remark.Text.Trim();
        //        recipe.UpdateTime = DateTime.Now;
        //        recipe.IsDelete = 1;
        //        if (recipesBLL.UpdateRecipe(recipe))
        //        {
        //            MessageBox.Show("更新成功！", "提示");
        //            ReLoadList?.Invoke(); // 触发列表刷新
        //            LogHelper.Info(this.GetType(), "更新" + recipe.RecipeName + "成功！");
        //            this.Close();
        //        }
        //        else
        //        {
        //            MessageBox.Show("更新失败，请重试。", "错误");
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show($"发生异常：{ex.Message}", "错误");
        //    }
        //}

        /// <summary>
        ///新增配方数据
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        //private void InsertRecipe()
        //{
        //    if (!ValidateForm())
        //        return;
        //    try
        //    {
        //        RecipesModel recipe = new RecipesModel
        //        {
        //            RecipeName = txt_recipeName.Text.Trim(),
        //            ProductId = (int)cbx_productName.SelectedValue,
        //            Recipe01 = txt_recipe01.Text.Trim(),
        //            Recipe02 = txt_recipe02.Text.Trim(),
        //            Recipe03 = txt_recipe03.Text.Trim(),
        //            Recipe04 = txt_recipe04.Text.Trim(),
        //            Recipe05 = txt_recipe05.Text.Trim(),
        //            Recipe06 = txt_recipe06.Text.Trim(),
        //            Recipe07 = txt_recipe07.Text.Trim(),
        //            Recipe08 = txt_recipe08.Text.Trim(),
        //            Recipe09 = txt_recipe09.Text.Trim(),
        //            Recipe10 = txt_recipe10.Text.Trim(),

        //            Recipe11 = txt_recipe11.Text.Trim(),
        //            Recipe12 = txt_recipe12.Text.Trim(),
        //            Recipe13 = txt_recipe13.Text.Trim(),
        //            Recipe14 = txt_recipe14.Text.Trim(),
        //            Recipe15 = txt_recipe15.Text.Trim(),
        //            Recipe16 = txt_recipe16.Text.Trim(),
        //            Recipe17 = txt_recipe17.Text.Trim(),
        //            Recipe18 = txt_recipe18.Text.Trim(),
        //            Recipe19 = txt_recipe19.Text.Trim(),
        //            Recipe20 = txt_recipe20.Text.Trim(),

        //            Remark = txt_remark.Text.Trim(),
        //            CreateTime = DateTime.Now,
        //            //UpdateTime = DateTime.Now,
        //            IsDelete = 1 // 默认为未删除
        //        };

        //        if (recipesBLL.InsertRecipe(recipe))
        //        {
        //            MessageBox.Show("新增成功！", "提示");
        //            ReLoadList?.Invoke(); // 触发列表刷新
        //            LogHelper.Info(this.GetType(), "新增配方" + recipe.RecipeName + "成功！");
        //            this.Close();
        //        }
        //        else
        //        {
        //            MessageBox.Show("新增失败，请重试。", "错误");
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show($"发生异常：{ex.Message}", "错误");
        //    }
        //}

        private void InsertRecipe()
        {
            if (!ValidateForm())
                return;

            try
            {
                var recipe = new RecipesModel
                {
                    RecipeName = txt_recipeName.Text.Trim(),
                    ProductId = (int)cbx_productName.SelectedValue,
                    CreateTime = DateTime.Now,
                    IsDelete = 1
                };

                // 批量设置配方字段
                SetRecipeFields(recipe);

                if (recipesBLL.InsertRecipe(recipe))
                {
                    MessageBox.Show("新增成功！", "提示");
                    ReLoadList?.Invoke();
                    LogHelper.Info(this.GetType(), "新增配方" + recipe.RecipeName + "成功！");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("新增失败，请重试。", "错误");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生异常：{ex.Message}", "错误");
            }
        }

        private void UpdateRecipe()
        {
            if (!ValidateForm())
                return;

            try
            {
                var recipe = recipesBLL.GetRecipesById(typeModel.FId);
                if (recipe == null)
                {
                    MessageBox.Show("找不到该配方信息！", "错误");
                    return;
                }

                recipe.RecipeName = txt_recipeName.Text.Trim();
                recipe.ProductId = (int)cbx_productName.SelectedValue;
                recipe.UpdateTime = DateTime.Now;
                recipe.IsDelete = 1;

                // 批量设置配方字段
                SetRecipeFields(recipe);

                if (recipesBLL.UpdateRecipe(recipe))
                {
                    MessageBox.Show("更新成功！", "提示");
                    ReLoadList?.Invoke();
                    LogHelper.Info(this.GetType(), "更新" + recipe.RecipeName + "成功！");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("更新失败，请重试。", "错误");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生异常：{ex.Message}", "错误");
            }
        }

        // 封装的核心方法：批量设置配方字段
        private void SetRecipeFields(RecipesModel recipe)
        {
            try
            {
                recipe.RecipeName = txt_recipeName.Text.Trim();
                recipe.ProductId = (int)cbx_productName.SelectedValue;
                recipe.WeldingNum = txt_weldingNum.Text.Trim();
                recipe.Recipe02 = txt_recipe02.Text.Trim();
                recipe.Recipe03 = txt_recipe03.Text.Trim();
                recipe.Recipe04 = txt_recipe04.Text.Trim();
                recipe.Recipe05 = txt_recipe05.Text.Trim();
                recipe.Recipe06 = txt_recipe06.Text.Trim();
                recipe.Recipe07 = txt_recipe07.Text.Trim();
                recipe.Recipe08 = txt_recipe08.Text.Trim();
                recipe.Recipe09 = txt_recipe09.Text.Trim();
                recipe.Recipe10 = txt_recipe10.Text.Trim();

                recipe.Recipe11 = txt_recipe11.Text.Trim();
                recipe.Recipe12 = txt_recipe12.Text.Trim();
                recipe.Recipe13 = txt_recipe13.Text.Trim();
                recipe.Recipe14 = txt_recipe14.Text.Trim();
                recipe.Recipe15 = txt_recipe15.Text.Trim();
                recipe.Recipe16 = txt_recipe16.Text.Trim();
                recipe.Recipe17 = txt_recipe17.Text.Trim();
                recipe.Recipe18 = txt_recipe18.Text.Trim();
                recipe.Recipe19 = txt_recipe19.Text.Trim();
                recipe.Recipe20 = txt_recipe20.Text.Trim();

                recipe.Remark = txt_remark.Text.Trim();
                recipe.UpdateTime = DateTime.Now;
                recipe.IsDelete = 1;
            }
            catch
            {
                MessageBox.Show("发生异常！请重试", "错误");
            }
        }
        /// <summary>
        /// 数据验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateForm()
        {
            if (string.IsNullOrWhiteSpace(txt_recipeName.Text))
            {
                MessageBox.Show("请输入配方名称！", "提示");
                txt_recipeName.Focus();
                return false;
            }

            if ((int)cbx_productName.SelectedValue <= 0)
            {
                MessageBox.Show("请选择一个产品！", "提示");
                cbx_productName.Focus();
                return false;
            }

            if(string.IsNullOrWhiteSpace(txt_remark.Text))
            {
                MessageBox.Show("请输入备注！", "提示");
                txt_remark.Focus();
                return false;
            }
            return true;
        }
    }
}
